package dao.impl;

import dao.CommentArrayDao;
import pojo.Comments;
import util.DataUtils;
import util.JdbcUtils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class CommentArrayDaoImpl implements CommentArrayDao {
    private Connection conn = null;
    private ResultSet rs = null;

    public ArrayList<Comments> findCommentChild(Integer cid) throws SQLException, ClassNotFoundException {
        conn = JdbcUtils.dbconn();
        ArrayList<Comments> commentsChild = new ArrayList<>();
        rs = JdbcUtils.selectsql("select * from comments where parent='" + cid + "'");
//        while (true) {
//                if (!rs.next()) break;
//                Comments comment = new Comments();
//                comment.setNid(rs.getInt("nid"));
//                comment.setUid(rs.getInt("uid"));
//                comment.setUsename(rs.getString("usename"));
//                comment.setComment(rs.getString("comment"));
//                comment.setCid(rs.getInt("cid"));
//                comment.setParent(rs.getInt("parent"));
//                commentsChild.add(comment);
//        }
        rs.last();
        int row = rs.getRow();
        rs.beforeFirst();
        do {
            row--;
            Comments comment = new Comments();
            comment = (Comments) DataUtils.dataEncapsulation(Class.forName("pojo.Comments"), rs);
            commentsChild.add(comment);
        } while (row > 0);
        JdbcUtils.closeconn(rs, null, conn);
        return commentsChild;
    }

}
